import peewee as pw


class Model(pw.Model):
    class Meta:
        only_save_dirty = True


class User(Model):
    account_id = pw.IntegerField(primary_key=True)
    name = pw.CharField(32)
    gender = pw.IntegerField(default=0)
    type = pw.IntegerField()
    is_manager = pw.BooleanField(default=False)
    password_ssha = pw.FixedCharField(64)
    password_salt = pw.FixedCharField(64)


class Course(Model):
    uuid = pw.FixedCharField(36, primary_key=True)
    name = pw.CharField(32, unique=True)
    description = pw.CharField(99, default='')


class ClassModel(Model):
    uuid = pw.FixedCharField(36, primary_key=True)
    name = pw.CharField(32, unique=True)
    description = pw.CharField(99, default='')
    course_uuid = pw.ForeignKeyField(Course, Course.uuid, on_delete='CASCADE')


class ClassUserBind(Model):
    class_uuid = pw.ForeignKeyField(ClassModel, ClassModel.uuid, on_delete='CASCADE', index=True)
    student_id = pw.ForeignKeyField(User, User.account_id, on_delete='CASCADE', index=True)


class Chapter(Model):
    uuid = pw.FixedCharField(36, primary_key=True)
    name = pw.CharField(32)
    description = pw.CharField(99, default='')
    course_uuid = pw.ForeignKeyField(Course, Course.uuid, on_delete='CASCADE', index=True)
    index = pw.IntegerField(default=1, index=True)
    content = pw.TextField(default='')


class Question(Model):
    uuid = pw.FixedCharField(36, primary_key=True)
    question = pw.TextField()
    analysis = pw.TextField(default='')
    answer_a = pw.TextField(default='')
    answer_b = pw.TextField(default='')
    answer_c = pw.TextField(default='')
    answer_d = pw.TextField(default='')
    answer_e = pw.TextField(default='')
    answer_f = pw.TextField(default='')
    answer_g = pw.TextField(default='')
    answer_h = pw.TextField(default='')
    is_a = pw.BooleanField(default=False)
    is_b = pw.BooleanField(default=False)
    is_c = pw.BooleanField(default=False)
    is_d = pw.BooleanField(default=False)
    is_e = pw.BooleanField(default=False)
    is_f = pw.BooleanField(default=False)
    is_g = pw.BooleanField(default=False)
    is_h = pw.BooleanField(default=False)
    is_true = pw.BooleanField(default=False)
    type = pw.IntegerField(default=0)
    level = pw.IntegerField(default=0)
    answer_count = pw.IntegerField(default=0)
    correct_count = pw.IntegerField(default=0)
    chapter_uuid = pw.ForeignKeyField(Chapter, Chapter.uuid, on_delete='CASCADE', index=True)
    is_exam_question = pw.BooleanField(default=False)


class QuestionAnswer(Model):
    student_id = pw.ForeignKeyField(User, User.account_id, on_delete='CASCADE', index=True)
    question_uuid = pw.ForeignKeyField(Question, Question.uuid, on_delete='CASCADE', index=True)
    used_time = pw.IntegerField()
    is_correct = pw.BooleanField(default=False)


def get_all_classes():
    return [User, Course, ClassModel, ClassUserBind, Chapter, Question, QuestionAnswer]
